package bitWork;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tao
 * Date: 2023-11-24
 * Time: 15:38
 */
public class ThreadDemo1 {
    private static int[] arr = new int[1000_0000];
    private static int count1 = 0;
    private static int count2 = 0;
    public static void main(String[] args) throws InterruptedException {
        //开始时间
        long start = System.currentTimeMillis();
        Random random = new Random();
        //1、数组先放元素
        for (int i = 0; i < 1000_0000; i++) {
            arr[i] = random.nextInt(100) + 1;
        }
        //2、两个线程分别去偶数和奇数下标的值，再求和
        Thread t1 = new Thread(() -> {
            //偶数下标
            for (int i = 0; i < 1000_0000; i += 2) {
                count1 += arr[i];
            }
        });
        Thread t2 = new Thread(() -> {
            //奇数下标
            for (int i = 1; i < 1000_0000; i += 2) {
                count2 += arr[i];
            }
        });
        t1.start();
        t2.start();

        t1.join();
        t2.join();
        //4、汇总
        int count = count1 + count2;
        //结束时间
        long end = System.currentTimeMillis();
        //5、总执行时间：
        System.out.println("耗时：" + (end - start) + "ms");
        System.out.println("count值：" + count);
    }
}
